﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class NonWorkingDayDAL : INonWorkingDayDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        
        public NonWorkingDayDAL()
        {
        }

        public IList<NonWorkingDayInfo> GetAllNonWorkingDays(string bg_year)
        {
            string strSQL = string.Empty;
            List<NonWorkingDayInfo> result = new List<NonWorkingDayInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);
                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bg_year", bg_year);
                //SELECT * FROM hrm_ps_non_working_days WHERE record_status not like 'D' AND bg_year = 2556 AND (non_working_date >= '2013-5-1' AND non_working_date <= '2013-5-31')
                strSQL = "select * from hrm_ps_non_working_days where record_status not like @delete_status";
                strSQL += (!string.IsNullOrEmpty(bg_year) ? " and bg_year >= @bg_year" : "");

                //DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                
                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    NonWorkingDayInfo info = new NonWorkingDayInfo(
                        rdr.GetInt32(0)
                        , rdr.IsDBNull(1) ? 0 : rdr.GetInt32(1)
                        , rdr.IsDBNull(2) ? (DateTime?)null : rdr.GetDateTime(2)
                        , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                        , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                        , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                        , rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6)
                        , rdr.IsDBNull(7) ? (DateTime?)null : rdr.GetDateTime(7)
                        , rdr.IsDBNull(8) ? (int?)null : rdr.GetInt32(8)
                        , rdr.IsDBNull(9) ? (DateTime?)null : rdr.GetDateTime(9)
                        );
                    result.Add(info);
                }

                return result;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                if (dbManager.Connection != null) {
                    dbManager.CloseReader();
                    dbManager.Dispose(); 
                }
            }
        }

        public IList<NonWorkingDayInfo> ListNonWorkingDay(NonWorkingDayInfo nwdInfo)
        {
            string strSQL;

            List<NonWorkingDayInfo> infoList = new List<NonWorkingDayInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Sequence", nwdInfo.Sequence);
                dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@Year", nwdInfo.Year);
                dbManager.AddParameters(3, "@Month", nwdInfo.Month);
                dbManager.AddParameters(4, "@Day", nwdInfo.Day);
                dbManager.AddParameters(5, "@BudgetYear", nwdInfo.BudgetYear);

                strSQL = "SELECT NON_WORKING_SEQ, BG_YEAR, NON_WORKING_DATE, NON_WORKING_NAME, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, UPDATED_BY"
                        + ", YEAR(NON_WORKING_DATE) + 543 AS NON_WORKING_YEAR"
                        + ", CASE MONTH(NON_WORKING_DATE) WHEN 1 THEN 'มกราคม'"
                            + " WHEN 2 THEN 'กุมภาพันธ์'"
                            + " WHEN 3 THEN 'มีนาคม'"
                            + " WHEN 4 THEN 'เมษายน'"
                            + " WHEN 5 THEN 'พฤษภาคม'"
                            + " WHEN 6 THEN 'มิถุนายน'"
                            + " WHEN 7 THEN 'กรกฎาคม'"
                            + " WHEN 8 THEN 'สิงหาคม'"
                            + " WHEN 9 THEN 'กันยายน'"
                            + " WHEN 10 THEN 'ตุลาคม'"
                            + " WHEN 11 THEN 'พฤศจิกายน'"
                            + " WHEN 12 THEN 'ธันวาคม'"
                            + " ELSE '' END AS NON_WORKING_MONTH"
                    + " FROM HRM_PS_NON_WORKING_DAYS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    //+ (nwdInfo.Year > 0 ? " AND YEAR(NON_WORKING_DATE) + 543 = @Year" : "")
                    + (nwdInfo.Month > 0 ? " AND MONTH(NON_WORKING_DATE) = @Month" : "")
                    + (nwdInfo.Day > 0 ? " AND DAY(NON_WORKING_DATE) = @Day" : "")
                    + (nwdInfo.Sequence > 0 ? " AND NON_WORKING_SEQ = @Sequence" : "")
                    + (nwdInfo.BudgetYear > 0 ? " AND BG_YEAR = @BudgetYear" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    NonWorkingDayInfo Info = new NonWorkingDayInfo(rdr.GetInt32(0)
                        , rdr.IsDBNull(1) ? 0 : rdr.GetInt32(1)
                        , rdr.IsDBNull(2) ? (DateTime?)null : rdr.GetDateTime(2)
                        , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                        , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                        , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                        , rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6)
                        , rdr.IsDBNull(7) ? (int?)null : rdr.GetInt32(7)
                        );
                    infoList.Add(Info);
                }

                return infoList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                       dbManager.Dispose();
            }
        }        

        public int Insert(NonWorkingDayInfo Info)
        {
            string strSQL;
            int NonWorkingSeq = 0;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@BudgetYear", Info.BudgetYear);
                dbManager.AddParameters(1, "@NonWorkingDay", Info.Date);
                dbManager.AddParameters(2, "@NonWorkingName", Info.Name);
                dbManager.AddParameters(3, "@Remarks", Info.Remarks);
                dbManager.AddParameters(4, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(5, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(6, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_PS_NON_WORKING_DAYS(BG_YEAR, NON_WORKING_DATE, NON_WORKING_NAME"
                        + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@BudgetYear, @NonWorkingDay, @NonWorkingName, @Remarks"
                        + ", @RecordStatus, @CreatedBy, @CreatedWhen)";

                strSQL += "; SELECT SCOPE_IDENTITY();";

                var dt = dbManager.ExecuteDataTable(CommandType.Text, strSQL);
                NonWorkingSeq = Convert.ToInt32(dt.Rows[0][0]);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }

            return NonWorkingSeq;
        }

        public void Update(NonWorkingDayInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@NonWorkingSeq", Info.Sequence);
                dbManager.AddParameters(1, "@BudgetYear", Info.BudgetYear);
                dbManager.AddParameters(2, "@NonWorkingDay", Info.Date);
                dbManager.AddParameters(3, "@NonWorkingName", Info.Name);
                dbManager.AddParameters(4, "@Remarks", Info.Remarks);
                dbManager.AddParameters(5, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(6, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_NON_WORKING_DAYS"
                           + " SET BG_YEAR = @BudgetYear"
                                + ", NON_WORKING_DATE = @NonWorkingDay"
                                + ", NON_WORKING_NAME = @NonWorkingName"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE NON_WORKING_SEQ = @NonWorkingSeq";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Close(); 
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@NonWorkingSeq", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdateWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_NON_WORKING_DAYS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdateWhen"
                    + " WHERE NON_WORKING_SEQ = @NonWorkingSeq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
